/*
 * @(#)DrawApplicationModel.java  1.0  June 10, 2006
 *
 * Copyright (c) 1996-2006 by the original authors of JHotDraw
 * and all its contributors ("JHotDraw.org")
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JHotDraw.org ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with
 * JHotDraw.org.
 */

package umage.core;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.JMenu;
import javax.swing.JToolBar;

import org.jhotdraw.app.Application;
import org.jhotdraw.app.DefaultApplicationModel;
import org.jhotdraw.app.Project;
import org.jhotdraw.draw.DefaultDrawingEditor;
import org.jhotdraw.draw.DrawingEditor;
import org.jhotdraw.draw.Figure;
import org.jhotdraw.util.ResourceBundleUtil;
import org.jhotdraw.xml.DOMStorable;

import umage.action.DefaultAction;
import umage.projects.UMageProject;

/**
 * DrawApplicationModel.
 * 
 * 
 * 
 * @author Werner Randelshofer.
 * @version 1.0 June 10, 2006 Created.
 */
public class UMageApplicationModel extends DefaultApplicationModel {

	private static final long serialVersionUID = -4051762740967736332L;

	/**
	 * This editor is shared by all projects.
	 */
	protected DefaultDrawingEditor sharedEditor;
	protected UMageButtonManager buttonManager;
	protected UMageMenuManager menuManager;

	/** Creates a new instance. */
	public UMageApplicationModel() {
	}

	@Override
	public void initApplication(Application a) {
		super.initApplication(a);
		UMageApplication app = (UMageApplication) a;
		app.addPropertyChangeListener("currentProject", new PropertyChangeListener() {
			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				UMageProject oldProject = (UMageProject) evt.getOldValue();
				UMageProject newProject = (UMageProject) evt.getNewValue();
				doProjectTransition(oldProject, newProject);
			}
		});
	}

	public DefaultDrawingEditor getSharedEditor() {
		if (sharedEditor == null) {
			sharedEditor = new DefaultDrawingEditor();
		}
		return sharedEditor;
	}

	public void initProject(Application a, Project p) {
		if (a.isSharingToolsAmongProjects()) {
			((UMageProject) p).setEditor(getSharedEditor());
		}
	}

	protected void doProjectTransition(UMageProject oldProject, UMageProject newProject) {
		buttonManager.updateVisibleButtons(newProject.getNeededFigures());
		menuManager.updateVisibleMenus(newProject.getNeededActions());
	}

	/**
	 * Creates toolbars for the application. This class always returns an empty
	 * list. Subclasses may return other values.
	 */
	public List<JToolBar> createToolBars(Application a, Project pr) {
		ResourceBundleUtil labels = ResourceBundleUtil.getLAFBundle("org.jhotdraw.draw.Labels");
		UMageProject p = (UMageProject) pr;

		DrawingEditor editor;
		if (p == null) {
			editor = getSharedEditor();
		} else {
			editor = p.getEditor();
		}

		// Load backend storables into DOM
		List<Class<DOMStorable>> storables = new PackageLoader<DOMStorable>().loadPackage("org.jhotdraw.draw",
				DOMStorable.class);
		for (Class<DOMStorable> storable : storables) {
			UMageFigureFactory.addStorableClass(storable);
		}

		// Load figures into DOM and Toolbars
		buttonManager = new UMageButtonManager(editor);
		List<Class<Figure>> figures = new PackageLoader<Figure>().loadPackage("umage.figures", Figure.class);
		for (Class<Figure> figure : figures) {
			UMageFigureFactory.addStorableClass(figure);
			buttonManager.addTool(figure);
		}

		return buttonManager.getToolbarList();
	}

	@Override
	public List<JMenu> createMenus(Application a, Project p) {
		menuManager = new UMageMenuManager((UMageApplication) a);
		List<Class<DefaultAction>> actions = new PackageLoader<DefaultAction>().loadPackage("umage.action",
				DefaultAction.class);
		for (Class<DefaultAction> action : actions) {
			menuManager.addAction(action);
		}

		return menuManager.getMenuList();
	}

}
